<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActionController::UrlWriter</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActionController::UrlWriter</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/actionpack/lib/action_controller/url_rewriter_rb.html">
                vendor/rails/actionpack/lib/action_controller/url_rewriter.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
In <b>routes.rb</b> one defines URL-to-controller mappings, but the reverse
is also possible: an URL can be generated from one of your routing
definitions. URL generation functionality is centralized in this module.
</p>
<p>
See <a href="Routing.html">ActionController::Routing</a> and <a
href="Resources.html">ActionController::Resources</a> for general
information about routing and routes.rb.
</p>
<p>
<b>Tip:</b> If you need to generate URLs from your models or some other
place, then <a href="UrlWriter.html">ActionController::UrlWriter</a> is
what you&#8216;re looking for. Read on for an introduction.
</p>
<h2>URL generation from parameters</h2>
<p>
As you may know, some functions - such as <a
href="Base.html#M001125">ActionController::Base#url_for</a> and
ActionView::Helpers::UrlHelper#link_to, can generate URLs given a set of
parameters. For example, you&#8216;ve probably had the chance to write code
like this in one of your views:
</p>
<pre>
  &lt;%= link_to('Click here', :controller =&gt; 'users',
          :action =&gt; 'new', :message =&gt; 'Welcome!') %&gt;

  #=&gt; Generates a link to: /users/new?message=Welcome%21
</pre>
<p>
link_to, and all other functions that require URL generation functionality,
actually use <a href="UrlWriter.html">ActionController::UrlWriter</a> under
the hood. And in particular, they use the <a
href="UrlWriter.html#M000863">ActionController::UrlWriter#url_for</a>
method. One can generate the same path as the above example by using the
following code:
</p>
<pre>
  include UrlWriter
  url_for(:controller =&gt; 'users',
          :action =&gt; 'new',
          :message =&gt; 'Welcome!',
          :only_path =&gt; true)
  # =&gt; &quot;/users/new?message=Welcome%21&quot;
</pre>
<p>
Notice the <tt>:only_path =&gt; true</tt> part. This is because <a
href="UrlWriter.html">UrlWriter</a> has no information about the website
hostname that your <a href="../Rails.html">Rails</a> app is serving. So if
you want to include the hostname as well, then you must also pass the
<tt>:host</tt> argument:
</p>
<pre>
  include UrlWriter
  url_for(:controller =&gt; 'users',
          :action =&gt; 'new',
          :message =&gt; 'Welcome!',
          :host =&gt; 'www.example.com')        # Changed this.
  # =&gt; &quot;http://www.example.com/users/new?message=Welcome%21&quot;
</pre>
<p>
By default, all controllers and views have access to a special version of
<a href="UrlWriter.html#M000863">url_for</a>, that already knows what the
current hostname is. So if you use <a
href="UrlWriter.html#M000863">url_for</a> in your controllers or your
views, then you don&#8216;t need to explicitly pass the <tt>:host</tt>
argument.
</p>
<p>
For convenience reasons, mailers provide a shortcut for <a
href="UrlWriter.html#M000863">ActionController::UrlWriter#url_for</a>. So
within mailers, you only have to type &#8216;<a
href="UrlWriter.html#M000863">url_for</a>&#8217; instead of &#8216;<a
href="UrlWriter.html#M000863">ActionController::UrlWriter#url_for</a>&#8217;
in full. However, mailers don&#8216;t have hostname information, and
what&#8216;s why you&#8216;ll still have to specify the <tt>:host</tt>
argument when generating URLs in mailers.
</p>
<h2>URL generation for named routes</h2>
<p>
<a href="UrlWriter.html">UrlWriter</a> also allows one to access methods
that have been auto-generated from named routes. For example, suppose that
you have a &#8216;users&#8217; resource in your <b>routes.rb</b>:
</p>
<pre>
  map.resources :users
</pre>
<p>
This generates, among other things, the method <tt>users_path</tt>. By
default, this method is accessible from your controllers, views and
mailers. If you need to access this auto-generated method from other places
(such as a model), then you can do that in two ways.
</p>
<p>
The first way is to include <a
href="UrlWriter.html">ActionController::UrlWriter</a> in your class:
</p>
<pre>
  class User &lt; ActiveRecord::Base
    include ActionController::UrlWriter         # !!!

    def name=(value)
      write_attribute('name', value)
      write_attribute('base_uri', users_path)   # !!!
    end
  end
</pre>
<p>
The second way is to access them through <a
href="UrlWriter.html">ActionController::UrlWriter</a>. The autogenerated
named routes methods are available as class methods:
</p>
<pre>
  class User &lt; ActiveRecord::Base
    def name=(value)
      write_attribute('name', value)
      path = ActionController::UrlWriter.users_path   # !!!
      write_attribute('base_uri', path)               # !!!
    end
  end
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000863">url_for</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000863" class="method-detail">
        <a name="M000863"></a>

        <div class="method-heading">
          <a href="#M000863" class="method-signature">
          <span class="method-name">url_for</span><span class="method-args">(options)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Generate a url based on the options provided, default_url_options and the
routes defined in routes.rb. The following options are supported:
</p>
<ul>
<li><tt>:only_path</tt> - If true, the relative url is returned. Defaults to
<tt>false</tt>.

</li>
<li><tt>:protocol</tt> - The protocol to connect to. Defaults to
&#8216;http&#8217;.

</li>
<li><tt>:host</tt> - Specifies the host the link should be targetted at. If
<tt>:only_path</tt> is false, this option must be provided either
explicitly, or via <tt>default_url_options</tt>.

</li>
<li><tt>:port</tt> - Optionally specify the port to connect to.

</li>
<li><tt>:anchor</tt> - An anchor name to be appended to the path.

</li>
<li><tt>:skip_relative_url_root</tt> - If true, the url is not constructed
using the <tt>relative_url_root</tt> set in
ActionController::Base.relative_url_root.

</li>
<li><tt>:trailing_slash</tt> - If true, adds a trailing slash, as in
&quot;/archive/2009/&quot;

</li>
</ul>
<p>
Any other key (<tt>:controller</tt>, <tt>:action</tt>, etc.) given to
<tt><a href="UrlWriter.html#M000863">url_for</a></tt> is forwarded to the
Routes module.
</p>
<p>
Examples:
</p>
<pre>
   url_for :controller =&gt; 'tasks', :action =&gt; 'testing', :host=&gt;'somehost.org', :port=&gt;'8080'    # =&gt; 'http://somehost.org:8080/tasks/testing'
   url_for :controller =&gt; 'tasks', :action =&gt; 'testing', :host=&gt;'somehost.org', :anchor =&gt; 'ok', :only_path =&gt; true    # =&gt; '/tasks/testing#ok'
   url_for :controller =&gt; 'tasks', :action =&gt; 'testing', :trailing_slash=&gt;true  # =&gt; 'http://somehost.org/tasks/testing/'
   url_for :controller =&gt; 'tasks', :action =&gt; 'testing', :host=&gt;'somehost.org', :number =&gt; '33'  # =&gt; 'http://somehost.org/tasks/testing?number=33'
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000863-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000863-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/url_rewriter.rb, line 129</span>
129:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">url_for</span>(<span class="ruby-identifier">options</span>)
130:       <span class="ruby-identifier">options</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">default_url_options</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>)
131: 
132:       <span class="ruby-identifier">url</span> = <span class="ruby-value str">''</span>
133: 
134:       <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:only_path</span>)
135:         <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> (<span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:protocol</span>) <span class="ruby-operator">||</span> <span class="ruby-value str">'http'</span>)
136:         <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value str">'://'</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">url</span>.<span class="ruby-identifier">match</span>(<span class="ruby-value str">&quot;://&quot;</span>)
137: 
138:         <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Missing host to link to! Please provide :host parameter or set default_url_options[:host]&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:host</span>]
139: 
140:         <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:host</span>)
141:         <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot;:#{options.delete(:port)}&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-identifier">:port</span>)
142:       <span class="ruby-keyword kw">else</span>
143:         <span class="ruby-comment cmt"># Delete the unused options to prevent their appearance in the query string.</span>
144:         [<span class="ruby-identifier">:protocol</span>, <span class="ruby-identifier">:host</span>, <span class="ruby-identifier">:port</span>, <span class="ruby-identifier">:skip_relative_url_root</span>].<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">k</span><span class="ruby-operator">|</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">k</span>) }
145:       <span class="ruby-keyword kw">end</span>
146:       <span class="ruby-identifier">trailing_slash</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:trailing_slash</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-identifier">:trailing_slash</span>)
147:       <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">ActionController</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">relative_url_root</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:skip_relative_url_root</span>]
148:       <span class="ruby-identifier">anchor</span> = <span class="ruby-node">&quot;##{CGI.escape options.delete(:anchor).to_param.to_s}&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:anchor</span>]
149:       <span class="ruby-identifier">generated</span> = <span class="ruby-constant">Routing</span><span class="ruby-operator">::</span><span class="ruby-constant">Routes</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">options</span>, {})
150:       <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> (<span class="ruby-identifier">trailing_slash</span> <span class="ruby-value">? </span><span class="ruby-identifier">generated</span>.<span class="ruby-identifier">sub</span>(<span class="ruby-regexp re">/\?|\z/</span>) { <span class="ruby-value str">&quot;/&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">$&amp;</span> } <span class="ruby-operator">:</span> <span class="ruby-identifier">generated</span>)
151:       <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">anchor</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">anchor</span>
152: 
153:       <span class="ruby-identifier">url</span>
154:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>